package com.hzgj.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/ELServlet")
public class ELServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //调用dopost方法
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //设置请求的字符集编码
        req.setCharacterEncoding("utf-8");
        //获取请求中的数据
        String user = req.getParameter("user");
        String pwd = req.getParameter("pwd");
        String [] hobbys = req.getParameterValues("hobby");
        //数组的遍历
        String str="";
        if(hobbys !=null){
            for (String ss:hobbys) {
                str= str+ss+",";
            }
        }
        //创建集合
        List<String> list=new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("麻溜");
        list.add("天气");
        //把数据保存作用域中
        req.setAttribute("user",user);
        req.setAttribute("pwd",pwd);
        req.setAttribute("str",str);
        req.setAttribute("list",list);
        //把数据传递到客户端页面
        req.getRequestDispatcher("/success.jsp").forward(req,resp);
    }
}
